package Exp2_ChenGY;

//PCI.java  
interface PCI {  
 void start();  
 void run();  
 void stop();  
}  

//GraphicsCard.java  
class GraphicsCard implements PCI {  
	 @Override  
	 public void start() {  
	     System.out.println("显卡启动");  
	 }  
	
	 @Override  
	 public void run() {  
	     System.out.println("显卡运行");  
	 }  
	
	 @Override  
	 public void stop() {  
	     System.out.println("显卡停止");  
	 }  
}  

//SoundCard.java  
class SoundCard implements PCI {   
 @Override  
	 public void start() {  
	     System.out.println("声卡启动");  
	 }  
	
	 @Override  
	 public void run() {  
	     System.out.println("声卡运行");  
	 }  
	
	 @Override  
	 public void stop() {  
	     System.out.println("声卡停止");  
	 }  
}  

//NetworkCard.java  
class NetworkCard implements PCI {  
	 @Override  
	 public void start() {  
	     System.out.println("网卡启动");  
	 }  
	
	 @Override  
	 public void run() {  
	     System.out.println("网卡运行");  
	 }  
	
	 @Override  
	 public void stop() {  
	     System.out.println("网卡停止");  
	 }  
}  

//Motherboard.java  
class Motherboard {  
 private PCI[] pciSlots = new PCI[5];  
 private int deviceCount = 0;  

 public void addDevice(PCI device) {  
     if (deviceCount < pciSlots.length) {  
         pciSlots[deviceCount++] = device;  
     }  
 }  

 public void startDevices() {  
     System.out.print("【开机】");  
     for (PCI device : pciSlots) {  
         if (device != null) {  
             device.start();  
             device.run();  
             System.out.print(" ");  
         }  
     }  
     System.out.println();  
 }  

 public void stopDevices() {  
     System.out.print("【关机】");  
     for (int i = deviceCount - 1; i >= 0; i--) {  
         if (pciSlots[i] != null) {  
             pciSlots[i].stop();  
             System.out.print(" ");  
         }  
     }  
     System.out.println();  
 }  
}  

//Computer.java  
public class Computer {  
 private Motherboard motherboard;  

 public Computer() {  
     motherboard = new Motherboard();  
     // 添加设备到主板  
     motherboard.addDevice(new GraphicsCard());  
     motherboard.addDevice(new SoundCard());  
     motherboard.addDevice(new NetworkCard());  
     // 假设只添加了3个设备，剩下的插槽为空  
 }  

 public static void main(String[] args) {  
     Computer computer = new Computer();  
     computer.motherboard.startDevices();  
     computer.motherboard.stopDevices();  
 }  
}